home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 …ember: Reference Library / Dev.CD Dec 00 RL Disk 1.toast / pc / technical documentation / develop / develop issue 27 / develop issue 27 code / internet config assistant / internetassistant / tmapview.cp < prev    next >
Encoding:
Text File  |  1996-05-11  |  5.8 KB  |  279 lines

  1. /*
  2.   File:            TMapView.cp
  3.  
  4.   Contains:        Class implementing a map location selector
  5.  
  6.   Written by:     Arno Gourdol
  7.  
  8.   Copyright:    Copyright 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10. */
  11.  
  12. #include "TMapView.h"
  13.  
  14. #include <Events.h>
  15. #include <Resources.h>
  16.  
  17.  
  18.  
  19. // --------------------------------------------------------------------
  20. // TMapView
  21. // --------------------------------------------------------------------
  22.  
  23. TMapView::TMapView(SInt16 mapID, SInt16 hilitedMapID, SInt16 colorMapID, 
  24.                 SInt16 colorHilitedMapID, SInt16 regionListID) :
  25.     fSelectedRegion(-1)
  26. {
  27.     // Build bitmap for the normal and hilited map 
  28.     fMap = new TBitmap(mapID, 1);
  29.     assert(fMap != NULL);
  30.     
  31.     fHilitedMap = new TBitmap(hilitedMapID, 1);
  32.     assert(fHilitedMap != NULL);
  33.     
  34.     if (mapID == colorMapID)
  35.     {
  36.         fColorMap = fMap;
  37.     }
  38.     else
  39.     {
  40.         fColorMap = new TBitmap(colorMapID, 8);
  41.         assert(fColorMap != NULL);
  42.     }
  43.     
  44.     if (hilitedMapID == colorHilitedMapID)
  45.     {
  46.         fColorHilitedMap = fHilitedMap;
  47.     }
  48.     else
  49.     {
  50.         fColorHilitedMap = new TBitmap(colorHilitedMapID, 8);
  51.         assert(fColorHilitedMap != NULL);
  52.     }
  53.     
  54.     // Initialize the region arrays
  55.     // The regions will be created at idle time    
  56.     {
  57.         RgnLResourceHandle rgnLResourceHandle = 
  58.             (RgnLResourceHandle) ::Get1Resource('rgnL', regionListID);
  59.  
  60.         fNumRegions = (**rgnLResourceHandle).regionCount;
  61.         
  62.         fRegions = (Region*)NewPtr(sizeof(Region) * fNumRegions);
  63.         assert(fRegions != NULL);
  64.         
  65.         for (int i = 0; i < fNumRegions; i++)
  66.         {
  67.             fRegions[i].pictureID = 
  68.                 (**rgnLResourceHandle).regions[i].pictureID;
  69.             fRegions[i].region = NULL;
  70.             fRegions[i].value = (**rgnLResourceHandle).regions[i].value;
  71.         }
  72.         
  73.         ::ReleaseResource((Handle)rgnLResourceHandle);
  74.     }
  75. }
  76.  
  77.  
  78.  
  79. // --------------------------------------------------------------------
  80. // ~TMapView
  81. // --------------------------------------------------------------------
  82.  
  83. TMapView::~TMapView()
  84. {
  85.     if (fMap != fColorMap)
  86.         delete fColorMap;
  87.     if (fHilitedMap != fColorHilitedMap)
  88.         delete fColorHilitedMap;
  89.     delete fMap;
  90.     delete fHilitedMap;
  91.  
  92.     if (fRegions != NULL)
  93.     {
  94.         for (int i = 0; i < fNumRegions; i++)
  95.         {
  96.             if (fRegions[i].region != NULL)
  97.                 DisposeRgn(fRegions[i].region);
  98.         }
  99.         DisposePtr((Ptr)fRegions);
  100.     }
  101. }
  102.  
  103.  
  104.  
  105. // --------------------------------------------------------------------
  106. // Draw
  107. // --------------------------------------------------------------------
  108.  
  109. void TMapView::Draw(const TDrawContext& drawContext, 
  110.                     const CRect& frame) const
  111. {
  112.     TBitmap* backgroundMap;
  113.     TBitmap* hilitedMap;
  114.     
  115.     if ((drawContext.GetDepth() >= 8) || 
  116.         (!drawContext.IsColor() && drawContext.GetDepth() == 4))
  117.     {
  118.         backgroundMap = fColorMap;
  119.         hilitedMap = fColorHilitedMap;
  120.     }
  121.     else
  122.     {
  123.         backgroundMap = fMap;
  124.         hilitedMap = fHilitedMap;
  125.     }
  126.  
  127.     // Draw the non-hilited map (background)
  128.     drawContext.DrawBitmap(backgroundMap, frame);
  129.     
  130.     // Draw the current region as hilited
  131.     if (fSelectedRegion >= 0 && fRegions[fSelectedRegion].region != NULL)
  132.     {
  133.         OffsetRgn(fRegions[fSelectedRegion].region, 
  134.                     frame.Left(), frame.Top());
  135.         drawContext.DrawBitmap(hilitedMap, frame, 
  136.                                 fRegions[fSelectedRegion].region);
  137.         OffsetRgn(fRegions[fSelectedRegion].region,
  138.                      -frame.Left(), -frame.Top());
  139.     }
  140. }
  141.  
  142.  
  143.  
  144. // --------------------------------------------------------------------
  145. // Pulse
  146. // --------------------------------------------------------------------
  147. // Hook function called at regular interval
  148. // At idle time, build the regions, if there are any remaining to be 
  149. // built
  150.  
  151. void TMapView::Pulse(void)
  152. {
  153.     for (int i = 0; i < fNumRegions; i++)
  154.     {
  155.         if (fRegions[i].region == NULL)
  156.         {
  157.             fRegions[i].region = NewRgn();
  158.             
  159.             if (fRegions[i].region != NULL)
  160.             {
  161.                 TBitmap regionBitmap(fRegions[i].pictureID, 1);
  162.                 if (regionBitmap.Lock())
  163.                 {
  164.                     ::BitMapToRegion(fRegions[i].region, 
  165.                         (BitMap*) *(regionBitmap.GetPixMapHandle()));
  166.                     regionBitmap.Unlock();
  167.                 }
  168.             }
  169.             break;
  170.         }
  171.     }
  172. }
  173.  
  174.  
  175.  
  176. // --------------------------------------------------------------------
  177. // FindRegion
  178. // --------------------------------------------------------------------
  179. // Returns the region in which "location" is, or -1
  180.  
  181. SInt16 TMapView::FindRegion(CPoint location)
  182. {
  183.     SInt16 result = -1;
  184.     
  185.     for (int i = 0; i < fNumRegions; i++)
  186.     {
  187.         if (fRegions[i].region != NULL && 
  188.             ::PtInRgn(location, fRegions[i].region))
  189.         {
  190.             result = i;
  191.             break;
  192.         }
  193.     }
  194.     
  195.     return result;
  196. }
  197.  
  198.  
  199.  
  200. // --------------------------------------------------------------------
  201. // SetValue
  202. // --------------------------------------------------------------------
  203. // Set the currently selected region
  204.  
  205. void TMapView::SetValue(SInt16 value)
  206. {
  207.     fSelectedRegion = -1;
  208.     for (int i = 0; i < fNumRegions; i++)
  209.     {
  210.         if (fRegions[i].value == value)
  211.         {
  212.             fSelectedRegion = i;
  213.             break;
  214.         }
  215.     }
  216. }
  217.  
  218.  
  219.  
  220. // --------------------------------------------------------------------
  221. // GetValue
  222. // --------------------------------------------------------------------
  223. // Return currently selected region
  224.  
  225. SInt16 TMapView::GetValue(void)
  226. {
  227.     SInt16 result = -1;
  228.     if (fSelectedRegion >= 0)
  229.     {
  230.         result = fRegions[fSelectedRegion].value;
  231.     }
  232.     return result;
  233. }
  234.  
  235.  
  236.  
  237. // --------------------------------------------------------------------
  238. // MouseDown
  239. // --------------------------------------------------------------------
  240.  
  241. void TMapView::MouseDown(CRect bounds)
  242. {
  243.     do
  244.     {
  245.         SInt16 selectedRegion;
  246.         
  247.         do 
  248.         {
  249.             CPoint location;
  250.             ::GetMouse(location);
  251.             location -= bounds.LeftTop();
  252.             selectedRegion = FindRegion(location);
  253.             
  254.             // Give some time to build additional masks if needed
  255.             Pulse();    
  256.         } while(selectedRegion == fSelectedRegion && StillDown());
  257.         
  258.         
  259.         // Update the map, using a draw context iterator
  260.         if (selectedRegion != fSelectedRegion)
  261.         {
  262.             fSelectedRegion = selectedRegion;
  263.  
  264.             TDrawContextIterator iterator(bounds);
  265.             
  266.             while (iterator != iterator.end())
  267.             {
  268.                 if ((*iterator).Lock())
  269.                 {
  270.                     Draw(*iterator, bounds);
  271.                     (*iterator).Unlock();
  272.                 }
  273.                 ++iterator;
  274.             }
  275.         }
  276.     } while (StillDown());
  277. }
  278.  
  279.